let app = getApp();
let utils = require("../../utils/util.js");
var api = require("../../mars/services/api.js");
let that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    group: {
      group_name: "",
      description: "",
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let answer = options.cupsNumber;
    that = this;
    this.setData({
      ["userInfo.answer"]: answer || ''
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  uploadInfo() {
    let params = {
      group_name: that.data.group.group_name,
      description: that.data.group.description
    }
    if (!params.group_name) {
      return wx.showToast({
        title: '组名不能为空',
        icon: "none"
      });
    }
    wx.showLoading({
      title: '保存中...',
    })

    api.WxRequest(api.saveGroup, params, (res) => {
      wx.hideLoading();
      if (res.status == 0 && res.data != -1) {
        console.log({
          groupId: res.data.id,
          userId: wx.getStorageSync('user').id,
          isAdmin: 1,
        });
        api.WxRequest(api.partInGroup, {
          groupId: res.data.id,
          userId: wx.getStorageSync('user').id,
          isAdmin: 1,
        }, (result) => {
          console.log(result);
          wx.showToast({
            title: '保存成功',
            icon: "none"
          });
          if (result.status) return;
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/communityIndex/communityIndex',
            });
          }, 500);
        });
        
      } else {
        wx.showToast({
          title: '该组名已经存在',
          icon: "none"
        });
      }
    })
  },
  bindInput(e) {
    let key = e.target.dataset.key;
    let value = e.detail.value;
    if (value) {
      that.setData({
        ['group.' + key]: value
      })
    }
  },
  // getLoginCode(cb) {
  //   return new Promise(function(resolve, reject) {
  //     wx.login({
  //       success: res => {
  //         // 发送 res.code 到后台换取 openId, sessionKey, unionId
  //         resolve(res.code);
  //       },
  //       fail() {
  //         reject(-1)
  //       }
  //     })
  //   })
  // }
})